<% content_for :title, "User Queries" %>

<div class="crayons-card p-6">
  <div class="flex justify-between items-center mb-6">
    <h1 class="crayons-title">User Queries</h1>
    <%= link_to "New User Query", new_admin_user_query_path, class: "crayons-btn crayons-btn--primary" %>
  </div>

  <!-- Search and Filter -->
  <div class="flex gap-4 mb-6">
    <%= form_with url: admin_user_queries_path, method: :get, local: true, class: "flex gap-4 items-end" do |f| %>
      <div class="flex flex-col">
        <%= f.label :search, "Search", class: "crayons-field__label" %>
        <%= f.text_field :search, value: params[:search], placeholder: "Search by name or description", class: "crayons-textfield" %>
      </div>
      
      <div class="flex flex-col">
        <%= f.label :active, "Status", class: "crayons-field__label" %>
        <%= f.select :active, [["All", ""], ["Active", true], ["Inactive", false]], { selected: params[:active] }, { class: "crayons-select" } %>
      </div>
      
      <%= f.submit "Filter", class: "crayons-btn crayons-btn--secondary" %>
    <% end %>
  </div>

  <!-- User Queries Table -->
  <div class="overflow-x-auto">
    <table class="crayons-table">
      <thead>
        <tr>
          <th>Name</th>
          <th>Description</th>
          <th>Created By</th>
          <th>Status</th>
          <th>Executions</th>
          <th>Last Executed</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <% @user_queries.each do |user_query| %>
          <tr>
            <td>
              <%= link_to user_query.name, admin_user_query_path(user_query), class: "crayons-link" %>
            </td>
            <td>
              <%= truncate(user_query.description, length: 100) if user_query.description.present? %>
            </td>
            <td>
              <%= user_query.created_by&.name || "Unknown" %>
            </td>
            <td>
              <span class="crayons-tag crayons-tag--<%= user_query.active? ? 'success' : 'secondary' %>">
                <%= user_query.active? ? 'Active' : 'Inactive' %>
              </span>
            </td>
            <td>
              <%= user_query.execution_count %>
            </td>
            <td>
              <%= user_query.last_executed_at&.strftime("%Y-%m-%d %H:%M") || "Never" %>
            </td>
            <td>
              <div class="flex gap-2">
                <%= link_to "View", admin_user_query_path(user_query), class: "crayons-btn crayons-btn--s crayons-btn--secondary" %>
                <%= link_to "Edit", edit_admin_user_query_path(user_query), class: "crayons-btn crayons-btn--s crayons-btn--secondary" %>
                <%= link_to user_query.active? ? "Deactivate" : "Activate", 
                    toggle_active_admin_user_query_path(user_query), 
                    method: :patch,
                    class: "crayons-btn crayons-btn--s crayons-btn--#{user_query.active? ? 'warning' : 'success'}" %>
                <%= link_to "Delete", admin_user_query_path(user_query), 
                    method: :delete, 
                    data: { confirm: "Are you sure you want to delete this user query?" },
                    class: "crayons-btn crayons-btn--s crayons-btn--danger" %>
              </div>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>

  <!-- Pagination -->
  <% if @user_queries.respond_to?(:current_page) %>
    <div class="flex justify-center mt-6">
      <%= paginate @user_queries %>
    </div>
  <% end %>
</div>

